package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.service.CustomerService;
import cn.wolfcode.crm.service.EmployeeService;
import cn.wolfcode.crm.service.SystemDictionaryitemService;
import cn.wolfcode.crm.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SystemDictionaryitemService systemDictionaryitemService;

    @RequestMapping("/potentialList")
    public String list(Model model , @ModelAttribute("qo" )CustomerQueryObject qo) {
            qo.setStatus(Customer.STATUS_POTENTIAL);



        // 当前登录用户
        Subject subject=SecurityUtils.getSubject();
        Employee customerEmployee = (Employee) subject.getPrincipal();
        // 不是超级管理员或者不是市场经理只能看自己跟踪的客户
      if (!(customerEmployee.isAdmin()|| subject.hasRole(Role.ROLE_SN_Market_Manager))) {
            qo.setSellerId(customerEmployee.getId());
        }


        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        List<Employee> sellers = employeeService.queryByRoleSn(new String[]{Role.ROLE_SN_Market_Manager, Role.ROLE_SN_Market});
        model.addAttribute("sellers", sellers);

      //  systemDictionaryitemService.queryBysystemDictionarysn();








        return "/customer/potentialList";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customerController:saveOrUpdate","潜在客户新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {

        try {
           if (customer.getId()!=null) {
               customerService.updateByPrimaryKey(customer);
           }else {
               customerService.insert(customer);
           }

       }catch (Exception e){
           return new JsonResult(false,"保存或者修改失败");
       }
        return new JsonResult(true,"保存或者修改成功");





    }



      /* @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"customerController:delete","潜在客户删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
       try {
           customerService.deleteByPrimaryKey(id);
       }catch (Exception e){
           return new JsonResult(false,"删除失败");

       }
        return new JsonResult(true,"删除成功");
    }*/
  /*  @RequestMapping("/input")
    @RequiresPermissions(value = {"customerController:input","潜在客户编辑"},logical = Logical.OR)
    public String input(Model model,Long id) {
        if (id != null) {
            Customer customer = customerService.selectByPrimaryKey(id);
            model.addAttribute("customer",customer);
        }
        return "/customer/input";

    }*/


}
